/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.SiterDao;
import com.zjdt.epserver.model.MyConstant;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dusin.util.DateUtil;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class SiterDaoImpl implements SiterDao{

    static Logger logger = Logger.getLogger(SiterDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public SiterDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }   
    
    
    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into siter(username,password,name,mobilePhone,note) values(?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from siter where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update siter set username=?,name=?,mobilePhone=?,note=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdatePassword(int id, String password) {
        boolean flag = true;
        String sql = null;
        sql = "update siter set password=? where id=?";
        int rows = jdbcTemplate.update(sql, password,id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public boolean doUpdatePasswordByUsername(String username, String password) {
        boolean flag = true;
        String sql = null;
        sql = "update siter set password=? where username=?";
        int rows = jdbcTemplate.update(sql, password,username);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public Map<String, Object> findById(int id) {
        String sql = null;
        try{
            sql = "select * from  siter where id = ?";
            return jdbcTemplate.queryForMap(sql,id);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }        
    }
    
    @Override
    public Map<String, Object> findByUsername(String username) {
        String sql = null;
        try{
            sql = "select * from  siter where username= ?";
            return jdbcTemplate.queryForMap(sql,username);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }        
    }
    
    @Override
    public Map<String, Object> findByName(String name) {
        String sql = null;
        try{
            sql = "select * from  siter where name = ?";
            List<Map<String,Object>> list= jdbcTemplate.queryForList(sql,name);        
            if(list.size()>0){  //只返回第一数据，假设数据库无同名人
                return list.get(0);
            }
            else
                return null;
        }catch(EmptyResultDataAccessException e){
            return null;
        }        
    }
   
    @Override
    public List<Map<String, Object>> findAll() {        
        String sql = "select * from vsiter";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        String sql;        
        sql = "select * from  vsiter where username = ?  and password=?";
        Map<String, Object> user=null;
        try{
            user = jdbcTemplate.queryForMap(sql,username,password);
        }catch(Exception e){            
        }
        return user; 
    }    

    @Override
    public boolean doUpdateFactory(int id, String fcode1,String fcode2,String fcode3,String fcode4) {
        boolean flag = true;
        String sql = null;
        sql = "update siter set fCode=?,fCode2=?,fCode3=?,fCode4=? where id=?";
        int rows = jdbcTemplate.update(sql, fcode1,fcode2,fcode3,fcode4,id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;        
    }
    
    
    @Override
    public Map<String,Object> findMyTrip(String factoryCode){
        String sql;        
        sql = "select * from  atrip where fcode = ?  and status=? and startTime>?";
        Map<String, Object> user=null;
        Calendar cal = Calendar.getInstance(Locale.PRC);
        cal.add(Calendar.DATE, -1); //过期时间，一天
        String timeLimit = DateUtil.get(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
        try{
            user = jdbcTemplate.queryForMap(sql,factoryCode,MyConstant.RUNNING,timeLimit);
        }catch(Exception e){            
        }
        return user; 
    }
    
    @Override
    public Map<String,Object> findMyFinishedTrip(String factoryCode){
        String sql;        
        sql = "select * from  atrip where fcode = ?  and status=? and startTime>?";
        Map<String, Object> user=null;
        Calendar cal = Calendar.getInstance(Locale.PRC);
        cal.add(Calendar.DATE, -1); //过期时间，一天
        String timeLimit = DateUtil.get(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
        try{
            user = jdbcTemplate.queryForMap(sql,factoryCode,MyConstant.FINISHED,timeLimit);
        }catch(Exception e){            
        }
        return user; 
    }
}
